package web.java.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import web.java.bean.Employee;
import web.java.daoImpl.EmployeeDaoImpl;

public class EmployeeListServlet extends HttpServlet {
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		EmployeeDaoImpl edi=new EmployeeDaoImpl();
		//得到报表总的行数，通过总行数计算总页数
		int count=edi.getCount();
		//默认每一页的行数
		int rows=5;
		//得到总页数
		int pageCount=0;
		if(count%rows==0){
			//正好整除的情况
			pageCount=count/rows;
		}else{
			//有余数的情况
			pageCount=count/rows+1;
		}
		int pages=0;
		String pagestr=request.getParameter("pages");
		if(pagestr==null){
			//第一次打开页面，默认是第一页
			pages=1;
		}else{
			//不是第一次打开，则把字符型转换为整型
			pages=Integer.parseInt(pagestr);
		}
		if(pages<1){
			//在当前页是第一页的情况，点击上一页
			pages=1;
		}
		if(pages>pageCount){
			//当前页是最后一页，点击下一页
			pages=pageCount;
		}
		//得到每页的数据
		System.out.println(pages);
		List<Employee> employeeList=edi.getPage((pages-1)*rows,rows);
		request.setAttribute("employeeList",employeeList);
		request.setAttribute("pages",pages);
		request.setAttribute("pageCount",pageCount);
		request.getRequestDispatcher("employeeList.jsp").forward(request,response);
	}

}
